package com.cqu.hospitalsystem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqu.hospitalsystem.entity.PatiCheck;
import com.cqu.hospitalsystem.service.PatiCheckService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import com.cqu.hospitalsystem.utils.result.code.Code;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("patiCheck")
@Tag(name = "PatiCheckController", description = "患者检查管理")
public class PatiCheckController {
    @Resource
    private PatiCheckService patiCheckService;

    @PostMapping("getPatiCheckAll")
    @Operation(summary = "获取所有患者检查", description = "查询所有患者检查")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到患者检查")
    })
    public DataResult getPatiCheckAll() {
        List<PatiCheck> patiPreslist = this.patiCheckService.queryAll();
        String result = JSON.toJSONString(patiPreslist);
        String jsStr = "{\"data\":" + result + "}";
        JSONObject js = JSONObject.parseObject(jsStr);
        DataResult dataResult = DataResult.successByData(js);
        dataResult.setcode(777);
        return dataResult;
    }

    @PostMapping("getCheckDetails")
    @Operation(summary = "获取检查详情", description = "根据患者检查ID查询检查详情")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到检查详情")
    })
    public DataResult getCheckDetails(@RequestBody @Parameter(description = "患者检查对象") PatiCheck patiCheck) {
        List<PatiCheck> patiPreslist = this.patiCheckService.queryByPId(patiCheck.getClistId());
        String result = JSON.toJSONString(patiPreslist);
        String jsStr = "{\"data\":" + result + "}";
        JSONObject js = JSONObject.parseObject(jsStr);
        DataResult dataResult = DataResult.successByData(js);
        dataResult.setcode(777);
        return dataResult;
    }

    @PostMapping("searchPatipres")
    @Operation(summary = "搜索患者检查", description = "根据患者标识搜索患者检查")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到患者检查")
    })
    public DataResult searchPatipres(@RequestBody @Parameter(description = "患者检查对象") PatiCheck patiCheck) {
        List<PatiCheck> patiCheck1 = this.patiCheckService.queryById(patiCheck.getPatientIdentify());
        String result = JSON.toJSONString(patiCheck1);
        String jsStr = "{\"data\":" + result + "}";
        JSONObject js = JSONObject.parseObject(jsStr);
        DataResult dataResult = DataResult.successByData(js);
        dataResult.setcode(777);
        return dataResult;
    }

    @PostMapping("updatelistState")
    @Operation(summary = "更新检查列表状态", description = "根据患者检查ID更新检查列表状态")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<PatiCheck> updatelistState(@RequestBody @Parameter(description = "患者检查对象") PatiCheck patiCheck) {
        System.out.println(patiCheck.getClistId());
        System.out.println(patiCheck.getCliststate());
        return ResponseEntity.ok(this.patiCheckService.update(patiCheck));
    }

    @PostMapping("updatecitemState")
    @Operation(summary = "更新检查项状态", description = "根据患者检查项ID更新检查项状态")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<PatiCheck> updatecitemState(@RequestBody @Parameter(description = "患者检查对象") PatiCheck patiCheck) {
        System.out.println(patiCheck.getItemListId());
        System.out.println(patiCheck.getCitemstate());
        return ResponseEntity.ok(this.patiCheckService.update_itemState(patiCheck));
    }

    @PostMapping("returnCheck")
    @Operation(summary = "退回检查", description = "根据患者检查项ID退回检查")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "退回成功"),
            @ApiResponse(responseCode = "400", description = "退回失败")
    })
    public ResponseEntity<PatiCheck> returnCheck(@RequestBody @Parameter(description = "患者检查对象") PatiCheck patiCheck) {
        System.out.println(patiCheck.getItemListId());
        System.out.println(patiCheck.getClistId());
        return ResponseEntity.ok(this.patiCheckService.update_checkState(patiCheck));
    }
}